package com.sx.mvvmlibs.base.impl

import android.app.Dialog
import android.widget.TextView
import androidx.annotation.IdRes
import androidx.annotation.LayoutRes
import com.sx.mvvmlibs.config.GlobalConfig

/**
 * @Author: YY
 * @Date: 2022/1/8
 * @Describe: 加载中对话框接口
 */
interface ILoadingDialog {

    /**
     * 显示加载中对话框
     */
    fun showLoadingDialog(dialog: Dialog, msg: String?) {
        dialog.setCancelable(isDialogCancelable())
        dialog.setCanceledOnTouchOutside(isDialogCancelOutside())
        dialog.show()
        // 只有在允许取消对话框和同时允许取消任务时，才有必要设置监听
        if (isDialogCancelable() || isDialogCancelOutside()) {
            dialog.setOnCancelListener { onCancelDialog() }
        }
        dialog.findViewById<TextView>(loadingDialogMsgId())?.text = msg
    }

    /**
     * LoadingDialog的布局
     */
    @LayoutRes
    fun loadingDialogLayout() = GlobalConfig.LoadingDialog.gLoadingDialogLayout

    /**
     * 文本消息Id
     */
    @IdRes
    fun loadingDialogMsgId() = GlobalConfig.LoadingDialog.gLoadingDialogMsgId

    /**
     * 隐藏Dialog
     */
    fun dismissLoadingDialog(dialog: Dialog) = dialog.dismiss()

    fun isDialogCancelable() = GlobalConfig.LoadingDialog.gDialogCancelable

    fun isDialogCancelOutside() = GlobalConfig.LoadingDialog.gDialogCancelOutside

    /**
     * 是否需要显示Dialog
     */
    fun isNeedLoadingDialog() = GlobalConfig.LoadingDialog.gIsNeedLoadingDialog

    fun onCancelDialog() {

    }

}